from multiprocessing import Process, Pool, Manager, Queue
import cherrypy
from Queue import Empty
from background import BackgroundTaskQueue

class JiraReportingSite:
    

    def __init__(self, bgtask):
        self.bgtask = bgtask
        return

    @cherrypy.expose
    def index(self):
        bgtask.put(test, ())
        return "Hello World"
"""
def start_server(queue):
    #bgtask = BackgroundTaskQueue(cherrypy.engine, queue)
    #bgtask.subscribe()
    cherrypy.config.update({'server.socket_host': '127.0.0.1'})
    cherrypy.config.update({'server.socket_port': 8081})
    cherrypy.config.update({'server.thread_pool': 10})
    cherrypy.quickstart(JiraReportingSite())
"""

def workerPool(queue):
    print "Hello"
    print queue
    pool = Pool(4)
    while 1:
        print queue.get()
        try:
            func, args, kwargs = queue.get(block=False, timeout=2)
            print func
        except Empty:
            continue 
        else:
            pool.apply_async(func, args)


def test(args):
    fp = open("test", "w")
    fp.write("TEST")
    fp.close()
    print "Test"

import os.path
tutconf = os.path.join(os.path.dirname(__file__), '../config/tutorial.conf')

if __name__ == "__main__":
    #manager = Manager()
    cherryQueue = Queue()
    bgtask = BackgroundTaskQueue(cherrypy.engine, cherryQueue)
    bgtask.subscribe()
    cherrypy.config.update({'server.socket_host': '127.0.0.1'})
    pool = Process(target=workerPool, args=(cherryQueue,))
    pool.start()
    cherrypy.quickstart(JiraReportingSite(bgtask))
    pool.join()
